/*
 * Copyright (C) 2012 JiangHongTiao
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package sk.lieskove.jianghongtiao.paris.core.classify;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.log4j.Logger;
import sk.lieskove.jianghongtiao.common.utils.PropertiesUtils;

/**
 * 
 * @author xjuraj e-mail: jjurco.sk_gmail.com
 */
public class JoinDocumentManager implements Serializable {
    
    private transient Logger log = Logger.getLogger(JoinDocumentManager.class);
    private transient PropertiesUtils pu = new PropertiesUtils(JoinDocumentManager.class);
    
    private Map<String, JoinDocumentClasses> join = new HashMap<String, JoinDocumentClasses>();

    public JoinDocumentManager(String joinString) {
        parseJoinString(joinString);
    }

    /**
     * parse join string. join string should be in format:
     * CLASS_1,...,CLASS_N[=>RESULT_CLASS_1];CLASS_12,...,CLASS_N2[=>RESULT_CLASS_2];
     * @param joinString join string for document types
     */
    private void parseJoinString(String joinString) {
        if(joinString == null){
            return;
        }
        for (String jClassString : joinString.split(";")) {
            JoinDocumentClasses joinDocumentClasses = new JoinDocumentClasses(jClassString);
            for (String docClass : joinDocumentClasses.getJoinClasses()) {
                join.put(docClass, joinDocumentClasses);
            }
        }
    }
    
    /**
     * join document classes to one result class and return joined classes
     * @param items items to join
     * @return joined document types (classes)
     */
    public <T> Map<String, List<T>> joinDocuments(Map<String, List<T>> items){
        if(join.isEmpty()){
            return items;
        }
        Map<String, List<T>> result = new HashMap<String, List<T>>();
        for (String item : items.keySet()) {
            if(join.containsKey(item)){
                result.put(join.get(item).getResultClass(), items.get(item));
            } else {
                result.put(item, items.get(item));
            }
        }
        return result;
    }
}
